package com.xiaomo.gitee.datamanager.service.temp;


import com.xiaomo.gitee.datamanager.mapper.TagsMapper;
import com.xiaomo.gitee.datamanager.entity.*;
import com.xiaomo.gitee.datamanager.mapper.ImagedesMapper;
import com.xiaomo.gitee.datamanager.mapper.ImagesMapper;
import com.xiaomo.gitee.datamanager.service.category.impl.ClassInfoServiceImpl;
import com.xiaomo.gitee.datamanager.service.tag.impl.ImageTagServiceImpl;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.util.*;
import java.util.stream.Collectors;

@Service
@NoArgsConstructor
public class PageServiceImpl implements PageService {

    private ImagesMapper imagesMapper;
    private ImagedesMapper imagedesMapper;
    private ImageTagServiceImpl imageTagService;
    private TagsMapper tagsMapper;
    private ClassInfoServiceImpl classInfoServiceImpl;

    @Value("${upload.dir}")
    private String uploadDirectory;


    @Autowired
    public PageServiceImpl(ImagesMapper imagesMapper, ImagedesMapper imagedesMapper, ClassInfoServiceImpl classInfoServiceImpl,
                           ImageTagServiceImpl imageTagService,TagsMapper tagsMapper) {
        this.imagesMapper = imagesMapper;
        this.imagedesMapper = imagedesMapper;
        this.classInfoServiceImpl = classInfoServiceImpl;
        this.imageTagService = imageTagService;
        this.tagsMapper = tagsMapper;
    }

    public void cindexPage(Model model) {
        List<Map<String,String>> p_list = new ArrayList<>();
        for (ClassInfo category : classInfoServiceImpl.getParentClasses()) {
            Map<String,String> map = new HashMap<>();
            map.put("class",category.getName());
            map.put("id",category.getId().toString());
            p_list.add(map);
        }
        model.addAttribute("p_classes",p_list);
        model.addAttribute("tags",tagsMapper.selectAll());
    }

    public void uploadPage(Model model) {
        class Option {
            public String value;
            public String label;
            public Option(String value, String label) {
                this.value = value;
                this.label = label;
            }
        }
        List<Option> optionsList = new ArrayList<>();
        for (ClassInfo category : classInfoServiceImpl.getParentClasses()) {
            optionsList.add(new Option(category.getId().toString(),category.getName()));
        }
        model.addAttribute("optionsList",optionsList);
        model.addAttribute("classInfo",new ClassInfo());
        model.addAttribute("cclassInfo",new CClassInfo());
        model.addAttribute("tags",tagsMapper.selectAll());
    }

    public void imagePage(Model model,String id) {
        Images images= imagesMapper.selectAllById(id);
        List<Imagedes> imagedes = Collections.singletonList(imagedesMapper.selectById(id));
        if (images == null || imagedes.isEmpty()) return;
        model.addAttribute("imageName", images.getName());
        model.addAttribute("imageDescription",imagedes.get(0).getDescription());
        model.addAttribute("imagePath","/datas/image/" + id);
        model.addAttribute("imageId",id);
        model.addAttribute("category", classInfoServiceImpl.getParentClassById(classInfoServiceImpl.getChildClassById(images.getClassification()).getP_class_id()).getName());

        List<Images> similarImages = imagesMapper.selectAllByClassification(images.getClassification());
        if (similarImages.isEmpty()) return;
        List<String> similarImagesPath = new ArrayList<>();
        model.addAttribute("totalNum",similarImages.size() - 1);
        model.addAttribute("id",id);
        int count = 0;
        for (Images images1 : similarImages) {
            if (count == 5) break;
            if (images1.getId().equals(id)) continue;
            similarImagesPath.add(images1.getId());
            count++;
        }
        model.addAttribute("similarImages",similarImagesPath);
        List<ImageTag> tagList = imageTagService.getTagsOfImage(id);
        List<Tags> resultTagList = new ArrayList<>();
        for (ImageTag imageTag : tagList) {
            resultTagList.add(tagsMapper.selectById(imageTag.getTagId()));
        }

        model.addAttribute("tags",resultTagList);
        List<Tags> allTags = tagsMapper.selectAll();
        List<Tags> totalTags = new ArrayList<>();
        Set<Long> tagsId = resultTagList.stream().map(Tags::getId).collect(Collectors.toSet());
        for (Tags tag : allTags) {
            if (tagsId.contains(tag.getId())) {
                continue;
            }
            totalTags.add(tag);
        }
        model.addAttribute("all_tags",totalTags);

        List<Tags> canDeleteTags = imageTagService.getTagsOfImage(id).stream()
                .filter(imageTag -> !imageTag.isBelongClass())
                .map(imageTag -> tagsMapper.selectOneById(imageTag.getTagId())).toList();
        //all_can_delete_tags
        model.addAttribute("all_can_delete_tags",canDeleteTags);

    }

}
